package tool

import (
	"fmt"
	"strconv"
)

type Bootstrap struct {
	Paginator
}

func (bt Bootstrap) getPreviousButton(text string) string {

	defText := "&laquo;"
	if text == "" {
		text = defText
	}

	if bt.currentPage <= 1 {
		return bt.getDisabledTextWrapper(text)
	}

	url := bt.url(bt.currentPage - 1)

	return bt.getPageLinkWrapper(url, text)

}

func (bt Bootstrap) getDisabledTextWrapper(text string) string {

	html := "<p class='pageEllipsis'>" + text + "</p>"

	return html
}

func (bt Bootstrap) getPageLinkWrapper(url string, page string) string {

	temp, _ := strconv.Atoi(page)
	if bt.currentPage == temp {
		return bt.getActivePageWrapper(page)
	}

	return bt.getAvailablePageWrapper(url, page)
}

func (bt Bootstrap) getActivePageWrapper(text string) string {
	html := "<a href='' class='cur'>"  + text + "</a>"
	return html
}

func (bt Bootstrap) getAvailablePageWrapper(url string, page string) string {
	html := "<a href='" + url + "' title='第'"+ page +"'页' >" + page +"</a>"
	return html
}

func (bt Bootstrap) getNextButton(text string) string {
	if !bt.hasMore {
		return bt.getDisabledTextWrapper(text)
	}
	url := bt.url(bt.currentPage + 1)

	return bt.getPageLinkWrapper(url, text)
}

func (bt Bootstrap) getUrlLinks(urls []string) string {
	html := ""
	for page, url := range urls {
		html += bt.getPageLinkWrapper(url, strconv.Itoa(page+1))
	}
	return html
}

func (bt Bootstrap) getDots() string {
	return bt.getDisabledTextWrapper("...")
}

func (bt Bootstrap) getLinks() string {

	block := map[string]interface{}{}

	side := 3
	window := side * 2

	if bt.lastPage < window+6 {
		block["first"] = bt.getUrlRange(1, bt.lastPage)
	} else if bt.currentPage <= window {
		block["first"] = bt.getUrlRange(1, window+2)
		block["last"] = bt.getUrlRange(bt.lastPage-1, bt.lastPage)
	} else if bt.currentPage > (bt.lastPage - window) {
		block["first"] = bt.getUrlRange(1, 2)
		block["last"] = bt.getUrlRange(bt.lastPage-(window+2), bt.lastPage)
	} else {
		block["first"] = bt.getUrlRange(1, 2)
		block["slider"] = bt.getUrlRange(bt.currentPage-side, bt.currentPage+side)
		block["last"] = bt.getUrlRange(bt.lastPage-1, bt.lastPage)
	}
	html := ""
	if _, ok := block["first"].([]string); ok {
		html += bt.getUrlLinks(block["first"].([]string))
	}

	if _, ok := block["slider"].([]string); ok {
		html += bt.getDots()
		html += bt.getUrlLinks(block["slider"].([]string))
	}

	if _, ok := block["last"].([]string); ok {
		html += bt.getDots()
		html += bt.getUrlLinks(block["last"].([]string))
	}
	return html
}

func (bt Bootstrap) Render() string {

	return fmt.Sprintf(
		"<ul class='pagination'>%s %s %s %s %s %s %s</ul>",
		bt.css(), bt.info(), bt.home(), bt.prev(), bt.getLinks(), bt.next(), bt.last())
}

//统计信息
func (bt Bootstrap) info() string {
	return "<a href='javascript:' title='共" + strconv.Itoa(bt.lastPage) + "页" + strconv.Itoa(bt.total) + "条'>共" + strconv.Itoa(bt.lastPage) + "页" + strconv.Itoa(bt.total) + "条</a>"
}

//统计信息
func (bt Bootstrap) home() string {
	if bt.currentPage > 1 {
		return "<a href='" + bt.url(1) + "' title='首页'>首页</a>"
	} else {
		return "<p>首页</p>"
	}
}

func (bt Bootstrap) prev() string {
	if bt.currentPage > 1 {
		return "<a href='" + bt.url(bt.currentPage-1) + "' title='上一页'>上一页</a>"
	} else {
		return "<p>上一页</p>"
	}
}

func (bt Bootstrap) next() string {
	if bt.hasMore {
		return "<a href='" + bt.url(bt.currentPage+1) + "' title='下一页'>下一页</a>"
	} else {
		return "<p>下一页</p>"
	}
}

func (bt Bootstrap) last() string {

	if bt.hasMore {
		return "<a href='"+bt.url(bt.lastPage)+"' title='尾页'>尾页</a>";
	} else {
		return "<p>尾页</p>";
	}

}



func (bt Bootstrap) css() string {
	return `<style type='text/css'>
        }.pagination p{
		margin:0;
		cursor:pointer
		}
		.pagination{
		/*height:20px;
		  padding:1px 5px;*/
		}
		.pagination a{
		display:block;
		float:left;
		margin-right:10px;
		padding:2px 12px;
		height:24px;
		border:1px #cccccc solid;
		background:#fff;
		text-decoration:none;
		color:#808080;
		font-size:12px;
		line-height:24px;
		}
		.pagination a:hover{
		color:#077ee3;
		background: white;
		border:1px #077ee3 solid;
		}
		.pagination a.cur{
		border:none;
		background:#077ee3;
		color:#fff;
		}
		.pagination p{
		float:left;
		padding:2px 12px;
		font-size:12px;
		height:24px;
		line-height:24px;
		color:#bbb;
		border:1px #ccc solid;
		background:#fcfcfc;
		margin-right:8px;
		}
		.pagination p.pageRemark{
		border-style:none;
		background:none;
		margin-right:0px;
		padding:4px 0px;
		color:#666;
		}
		.pagination p.pageRemark b{
		color:red;
		}
		.pagination p.pageEllipsis{
		border-style:none;
		background:none;
		padding:4px 0px;
		color:#808080;
		}
		.dates li {font-size: 14px;margin:20px 0}
		.dates li span{float:right}
		</style>`
}
